// pages/zhuce/zhuce.js
const urlApi = require('../../utils/server.api.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    patientFileDto: {
      patientId: "",
      emergencyContactName: "",
      emergencyContactPhone: "",
      emergencyContactRelation: "",
      leftEarHearing: "",
      rightEarHearing: "",
      leftVision: 0,
      rightVision: 0,
      height: 0,
      weight: 0,
      bloodType: "",
      personalInfo: "",
      familyInfo: "",
    },
    leftEarHearing: ["正常", "耳聋"],
    leftIndex: -1,
    rightEarHearing: ["正常", "耳聋"],
    rightIndex: -1,
    bloodType: ["A", "B", "AB", "O"],
    bloodIndex: -1,
    emergencyContactName: "",
    emergencyContactPhone: "",
    emergencyContactRelation: "",
    leftVision: "",
    rightVision: "",
    height: "",
    weight: "",
    personalInfo: "",
    familyInfo: "",
    verfiyCode: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this
    this.data.patientFileDto.patientId = app.globalData.userInfo.patientId
    wx.request({
      url: urlApi.getPatientFile() + "/" + app.globalData.userInfo.patientId,
      method: "get",
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            leftIndex: (that.data.leftEarHearing || []).findIndex((item) => item === res.data.data.leftEarHearing),
            rightIndex: (that.data.rightEarHearing || []).findIndex((item) => item === res.data.data.rightEarHearing),
            emergencyContactName: res.data.data.emergencyContactName,
            emergencyContactPhone: res.data.data.emergencyContactPhone,
            emergencyContactRelation: res.data.data.emergencyContactRelation,
            leftVision: res.data.data.leftVision,
            rightVision: res.data.data.rightVision,
            height: res.data.data.height,
            weight: res.data.data.weight,
            bloodIndex: (that.data.bloodType || []).findIndex((item) => item === res.data.data.bloodType),
            personalInfo: res.data.data.personalInfo,
            familyInfo: res.data.data.familyInfo
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  create_patientFile: function (e) {
    if (e.detail.value["emergencyContactName"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "紧急联系人不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["emergencyContactPhone"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "紧急联系人电话不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (!/^1[3456789]\d{9}$/.test(e.detail.value["emergencyContactPhone"])) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "紧急联系人电话格式错误",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["emergencyContactRelation"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "紧急联系人关系不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["leftIndex"] == -1) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "左耳听力不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["rightIndex"] == -1) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "右耳听力不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (Number(e.detail.value["leftVision"]) > 5.2 || Number(e.detail.value["leftVision"]) < 0) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "左眼视力不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (Number(e.detail.value["rightVision"]) > 5.2 || Number(e.detail.value["rightVision"]) < 0) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "右眼视力不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (Number(e.detail.value["height"]) == 0) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "身高不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (Number(e.detail.value["weight"]) == 0) {
      this.data.verfiyCode++;
      wx.showToast({
        title: "体重不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["bloodIndex"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "血型不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["personalInfo"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "个人史不能为空",
        icon: 'none',
        duration: 2000
      })
    } else
    if (e.detail.value["familyInfo"] == "") {
      this.data.verfiyCode++;
      wx.showToast({
        title: "家族史不能为空",
        icon: 'none',
        duration: 2000
      })
    } else {
      this.data.patientFileDto.emergencyContactName = e.detail.value["emergencyContactName"],
      this.data.patientFileDto.emergencyContactPhone = e.detail.value["emergencyContactPhone"],
      this.data.patientFileDto.emergencyContactRelation = e.detail.value["emergencyContactRelation"],
      this.data.patientFileDto.leftEarHearing = this.data.leftEarHearing[this.data.leftIndex],
      this.data.patientFileDto.rightEarHearing = this.data.rightEarHearing[this.data.rightIndex],
      this.data.patientFileDto.bloodType = this.data.bloodType[this.data.bloodIndex],
      this.data.patientFileDto.leftVision = Number(e.detail.value["leftVision"]),
      this.data.patientFileDto.rightVision = Number(e.detail.value["rightVision"]),
      this.data.patientFileDto.height = Number(e.detail.value["height"]),
      this.data.patientFileDto.weight = Number(e.detail.value["weight"]),
      this.data.patientFileDto.personalInfo = e.detail.value["personalInfo"],
      this.data.patientFileDto.familyInfo = e.detail.value["familyInfo"]
    }
    if (this.data.verfiyCode == 0) {
      wx.request({
        url: urlApi.addOrUpdatePatientFile(),
        method: "post",
        header: {
          'content-type': 'application/json'
        },
        data: this.data.patientFileDto,
        success: function (res) {
          if (res.data.code == 200) {
            app.globalData.userInfo.isFinal = 1
            wx.showToast({
              title: "成功",
              duration: 2000
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    } 
  },
  
  bindLeftChange: function(options) {
    this.setData ({
      leftIndex: options.detail.value
    })
  },

  bindRightChange: function(e) {
    this.setData ({
      rightIndex: e.detail.value
    })
  },

  bindBloodChange: function(e) {
    this.setData ({
      bloodIndex: e.detail.value
    })
  }
})